package action.contract;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import model.TContract;
import model.app.AppInfo;
import model.company.Companybasic;
import model.company.Companycontact;
import model.company.Companytopmanager;
import model.personal.Personalbasic;

import exception.Exceptions;

import service.app.InfoService;
import service.contract.ContractService;
import service.contract.ShowContractService;
import tool.WordDownload;
import util.Time;
import util.Tool;

public class ContractPrintAction {
	private String fileName;
	private Integer id;
	private ShowContractService showContractService;
	private ContractService contractService;
	private InfoService infoService;
	public InputStream getInputStream() throws Exceptions {
		TContract contractloan=showContractService.get_ht(id);
		String contractid=contractloan.getContractId();
		fileName=Tool.toChinese(contractid+".doc");
		Map<String, String> map=new HashMap<String, String>();
		map.put("contractid", contractid);//显示的东西
		map.put("startdate", Time.Timestamp2DateString(contractloan.getStartdate()));
		map.put("enddate", Time.Timestamp2DateString(contractloan.getEnddate()));
		map.put("applymoney",Tool.translateMoney(contractloan.getApplymoney()));
		map.put("loanpurpose",contractloan.getLoanpurpose());
		
		BigDecimal aa=new BigDecimal(contractloan.getPerformrates()).setScale(5, BigDecimal.ROUND_HALF_UP);
		map.put("rate",aa.toString());
		map.put("loangrant", contractloan.getLoangrant());
		if(contractloan.getClienttype().equals("1")){
			Personalbasic personal=new Personalbasic();
			personal=contractService.get_personalbasic(contractloan.getClientid());
			map.put("customname",personal.getName());
			map.put("address",personal.getCurrentresidentialaddress());
			map.put("phone",personal.getHomephone());
			map.put("fax","");	
			map.put("representative","");
			map.put("position","");
		}
		if(contractloan.getClienttype().equals("2")){
			Companybasic company=new Companybasic();
			company=contractService.get_companybasic(contractloan.getClientid());
			Companycontact companycontact=contractService.get_companycontact(contractloan.getClientid());
			map.put("customname",company.getName());
			map.put("address",company.getRegaddress());
			map.put("phone",companycontact.getTelephone());
			map.put("fax",companycontact.getFax());
			Companytopmanager topmanager=contractService.get_topmanager("cid="+company.getId()+"and category=1");
			map.put("representative",topmanager.getName());
			map.put("position","法人代表");
		}
		map.put("companyname",infoService.getInfo("companyname"));
		map.put("companyaddress", infoService.getInfo("address"));
		map.put("companyphone", infoService.getInfo("phone"));
		map.put("companyfax", infoService.getInfo("fax"));
		WordDownload www=new WordDownload();
		www.setWord(map,"htloan");//ht存在的WORD的名字
        return www.run();
	}
	
	public String execute(){
		return "success";
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public ShowContractService getShowContractService() {
		return showContractService;
	}

	public void setShowContractService(ShowContractService showContractService) {
		this.showContractService = showContractService;
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	public InfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(InfoService infoService) {
		this.infoService = infoService;
	}
}
